Skip to content

Conversation

@Will-ShaoHua
Copy link
Contributor

@Will-ShaoHua Will-ShaoHua commented Jan 14, 2025

Description

Now there are 2 ways to use NoOpTelemetryService

  1. if devs are not interested in validating the metric contents, don't need do anything as default test implementation is NoOpTelemetryService
  2. if devs are interested in the metric contents, can use MockTelemetryServiceExtension which will inject a spy of batcher and replace the original test implementation in junit before hook and revert it in after hook so that you can use ArgumentCaptor to capture what's going through the batcher

Relevant #5207

root cause:

Originally by doing class NoOpTelemetryService : TelemetryService(publisher, spy(DefaultTelemetryBatcher(publisher))) {, it's not 100% guaranteed that the TelemetryBatcher passed in is a mockito spy, and it result in NotAMockException thrown from these cases.

solution

Manually inject spy batcher separately in a different step instead of IDE doing the work when initializing app/project service components

License

I confirm that my contribution is made under the terms of the Apache 2.0 license.

@Will-ShaoHua Will-ShaoHua changed the title fix test failure caused by NotAMock error fix test failure caused by mockito notAMockException Jan 14, 2025
@Will-ShaoHua Will-ShaoHua changed the title fix test failure caused by mockito notAMockException try fix test failure caused by mockito notAMockException Jan 14, 2025
@Will-ShaoHua Will-ShaoHua requested a review from rli January 14, 2025 20:05
<applicationService serviceInterface="migration.software.aws.toolkits.jetbrains.services.telemetry.TelemetryService"
serviceImplementation="software.aws.toolkits.jetbrains.services.telemetry.DefaultTelemetryService"
testServiceImplementation="software.aws.toolkits.jetbrains.services.telemetry.NoOpTelemetryService"/>
serviceImplementation="software.aws.toolkits.jetbrains.services.telemetry.DefaultTelemetryService"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep it as no-op so that tests dont send metrics by default, but you can add the rule if you want to assert metrics?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha that makes sense

private val mockTelemetryService: NoOpTelemetryService by lazy { NoOpTelemetryService(publisher, batcher) }

override fun before() {
ApplicationManager.getApplication().replaceService(TelemetryService::class.java, mockTelemetryService, mockTelemetryService)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaceService needs to be reverted in the after()

@Will-ShaoHua Will-ShaoHua requested a review from rli January 16, 2025 18:36
@Will-ShaoHua Will-ShaoHua marked this pull request as ready for review January 16, 2025 18:36
@Will-ShaoHua Will-ShaoHua requested a review from a team as a code owner January 16, 2025 18:36

override fun after() {
reset(batcher())
Disposer.dispose(disposableParent)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rli is it sufficient for the purpose of reverting replaceService?

@Will-ShaoHua Will-ShaoHua changed the title try fix test failure caused by mockito notAMockException test(amazon Q): fix test failure caused by MockTelemetryExtension throwing NotAMockException Jan 16, 2025
@Will-ShaoHua Will-ShaoHua merged commit 5bceb99 into aws:main Jan 21, 2025
11 checks passed
@Will-ShaoHua Will-ShaoHua deleted the mockTelemetryService branch January 21, 2025 02:35
karanA-aws pushed a commit to karanA-aws/aws-toolkit-jetbrains that referenced this pull request Jan 22, 2025
…owing NotAMockException (aws#5258)

## Description
<!--- Describe your changes in detail -->
<!--- If appropriate, providing screenshots will help us review your contribution -->
<!--- If there is a related issue, please provide a link here -->
Now there are 2 ways to use NoOpTelemetryService
1. if devs are not interested in validating the metric contents, don't need do anything as default test implementation is NoOpTelemetryService
2. if devs are interested in the metric contents, can use `MockTelemetryServiceExtension` which will inject a spy of batcher and replace the original test implementation in junit before hook and revert it in after hook so that you can use ArgumentCaptor to capture what's going through the batcher


Relevant aws#5207 

### root cause:
Originally by doing `class NoOpTelemetryService : TelemetryService(publisher, spy(DefaultTelemetryBatcher(publisher))) {`, it's not 100% guaranteed that the TelemetryBatcher passed in is a mockito spy, and it result in `NotAMockException` thrown from these cases.

### solution
Manually inject spy batcher separately in a different step instead of IDE doing the work when initializing app/project service components---------

Co-authored-by: Richard Li <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants